/**
 * 
 */
package com.wm.service.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.Collect;
import com.wm.bean.Restaurant;
import com.wm.bean.RestaurantScriptoriumCon;
import com.wm.bean.RestaurantTime;
import com.wm.bean.Scriptorium;
import com.wm.dao.AreaDAO;
import com.wm.dao.CityDAO;
import com.wm.dao.CollectDAO;
import com.wm.dao.RestaurantDAO;
import com.wm.dao.RestaurantScriptoriumConDAO;
import com.wm.dao.RestaurantTimeDAO;
import com.wm.dao.ScriptoriumDAO;
import com.wm.service.AreaService;
import com.wm.service.CityService;
import com.wm.service.RestaurantScriptoriumConService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;

/**
 * @author zhangbaochao
 *
 */
public class RestaurantScriptoriumConServiceImp implements RestaurantScriptoriumConService {
	
	private RestaurantScriptoriumConDAO restaurantScriptoriumConDAO;
	private ScriptoriumDAO scriptoriumDAO;
	private RestaurantDAO restaurantDAO;
	private RestaurantTimeDAO restaurantTimeDAO;

	public RestaurantTimeDAO getRestaurantTimeDAO() {
		return restaurantTimeDAO;
	}

	public void setRestaurantTimeDAO(RestaurantTimeDAO restaurantTimeDAO) {
		this.restaurantTimeDAO = restaurantTimeDAO;
	}

	public RestaurantDAO getRestaurantDAO() {
		return restaurantDAO;
	}

	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}

	public RestaurantScriptoriumConDAO getRestaurantScriptoriumConDAO() {
		return restaurantScriptoriumConDAO;
	}

	public void setRestaurantScriptoriumConDAO(
			RestaurantScriptoriumConDAO restaurantScriptoriumConDAO) {
		this.restaurantScriptoriumConDAO = restaurantScriptoriumConDAO;
	}
	
	public ScriptoriumDAO getScriptoriumDAO() {
		return scriptoriumDAO;
	}

	public void setScriptoriumDAO(ScriptoriumDAO scriptoriumDAO) {
		this.scriptoriumDAO = scriptoriumDAO;
	}

	/**
	 * 保存RestaurantScriptoriumCon对象
	 * 
	 * @param restaurantScriptoriumCon
	 * 			RestaurantScriptoriumCon
	 * @return RestaurantScriptoriumCon
	 * 			RestaurantScriptoriumCon
	 */
	public RestaurantScriptoriumCon saveRestaurantScriptoriumCon(RestaurantScriptoriumCon restaurantScriptoriumCon){
		return this.getRestaurantScriptoriumConDAO().saveRestaurantScriptoriumCon(restaurantScriptoriumCon);
	}
	
	/**
	 * 更新RestaurantScriptoriumCon对象
	 * 
	 * @param restaurantScriptoriumCon
	 * 			RestaurantScriptoriumCon
	 * @return RestaurantScriptoriumCon
	 * 			RestaurantScriptoriumCon
	 */
	public RestaurantScriptoriumCon updateRestaurantScriptoriumCon(RestaurantScriptoriumCon restaurantScriptoriumCon){
		return this.getRestaurantScriptoriumConDAO().updateRestaurantScriptoriumCon(restaurantScriptoriumCon);
	}
	
	/**
	 * 根据ID删除RestaurantScriptoriumCon对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeRestaurantScriptoriumConById(long id){
		this.getRestaurantScriptoriumConDAO().removeRestaurantScriptoriumConById(id);
	}
	
	/**
	 * 根据ID获取RestaurantScriptoriumCon对象
	 * 
	 * @param id
	 * 			long
	 * @return RestaurantScriptoriumCon
	 * 			RestaurantScriptoriumCon
	 */
	public RestaurantScriptoriumCon findRestaurantScriptoriumConById(long id){
		return this.getRestaurantScriptoriumConDAO().findRestaurantScriptoriumConById(id);
	}
	
	/**
	 * 根据参数获取RestaurantScriptoriumCon对象列表
	 * 
	 * @param resId
	 * 			long
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			List
	 */
	public List findRestaurantScriptoriumConsByResIdIsShow(long resId,int isShow, String orderby, int ascOrDesc){
		List l = this.getRestaurantScriptoriumConDAO().findRestaurantScriptoriumConsByResIdIsShow(resId, isShow, orderby, ascOrDesc);
		if(l == null){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				RestaurantScriptoriumCon c = (RestaurantScriptoriumCon) l.get(i);
				Scriptorium s = this.getScriptoriumDAO().findScriptoriumById(c.getScrId());
				c.setScriptorium(s);
			}
			return l;
		}
	}
	
	/**
	 * 根据参数获取RestaurantScriptoriumCon对象列表
	 * @param collectList
	 * @param scrId
	 * @param isShow
	 * @param orderby
	 * @param ascOrDesc
	 * @return
	 */
	public List findRestaurantScriptoriumConsByScrIdIsShowCollectList(long scrId,int isShow, String orderby, int ascOrDesc ,List collectList){
		List l = this.getRestaurantScriptoriumConDAO().findRestaurantScriptoriumConsByScrIdIsShow(scrId, isShow, orderby, ascOrDesc);
		if(l == null){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				RestaurantScriptoriumCon c = (RestaurantScriptoriumCon) l.get(i);
				Restaurant r = this.getRestaurantDAO().findRestaurantByIdValidIsShow(c.getResId(),1,1);
				//用户是否收藏
				if(collectList!=null && collectList.size()>0){
					for(int j=0;j<collectList.size();j++){
						Collect collect = (Collect)collectList.get(j);
						if(collect.getObjId()==c.getResId() && collect.getObjType()==0){
							r.setIsCollect(1);
						}
					}
				}
				//开业时间
				r.setTimeList(this.getRestaurantTimeDAO().findRestaurantTimesByResIdIsShow(c.getResId(), 1, "startHour", 0));
				r.setIsBusiness(this.compareTime(r.getTimeList()));
				c.setRestaurant(r);
			}
			return l;
		}
	}
	
	public static int compareTime(List timeList){
		int r=0;
		if(timeList!=null){ 
			//设置时区
			Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT+8")); 
			int hours = c.get(Calendar.HOUR_OF_DAY); 
			int minutes = c.get(Calendar.MINUTE);
			for(int i=0;i<timeList.size();i++){
				RestaurantTime rt = (RestaurantTime)timeList.get(i);
				if(hours > rt.getStartHour() && hours < rt.getEndHour()){
					r = 1;
					break;
				}else if(hours == rt.getStartHour() && hours < rt.getEndHour()){
					r = 1;
					break;
				}else if(hours == rt.getEndHour() && minutes < rt.getEndMinute()){
					r = 1;
					break;
				}
			}
			System.out.println(r+"  "+hours+"  "+minutes);
		}
		return r;
	}
}
